package com.lecture.java8.datastructure;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2021/3/25 8:36
 */
public class Demo7 {

    public static void main(String[] args) {
        int[] arr = {1,-5,8, 3,-4,15,-8};
        fun1(arr, arr.length);
        fun2(arr, arr.length);
        func3(arr);
    }

    public static void fun1(int[] arr, int n) {
        int thisSum, maxSum = 0;
        for (int i = 0; i < n; i++) {
            thisSum = 0;
            for (int j = i; j < n; j++) {
                thisSum += arr[j];
                if (thisSum > maxSum) {
                    maxSum = thisSum;
                }
            }
        }
        System.out.println(maxSum);
    }
    public static void fun2(int[] a, int n) {
        int thisSum, maxSum = 0;
        for (int i = 0; i < n; i++) {//i是最左端的位置
            thisSum = 0;
            for (int j = i; j < n; j++) {//j是最右端的位置
                thisSum += a[j];
                if (thisSum > maxSum) {
                    maxSum = thisSum;
                }
            }
        }
        System.out.println(maxSum);
    }

    public static void func3(int[] arr) {
        int thisSum=0, maxSum = 0;
        for (int i = 0; i < arr.length; i++) {
            thisSum += arr[i];
            if (thisSum > maxSum) {
                maxSum = thisSum;
            } else if(thisSum < 0) {
                thisSum = 0;
            }
        }
        System.out.println(maxSum);
    }
}
